package com.junzhiit.nengji.app.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.app.service.AuthorizeService;
import com.junzhiit.nengji.app.web.util.aliyun.SmsUtil;
import com.junzhiit.nengji.app.web.vo.LoginVO;
import com.junzhiit.nengji.app.web.vo.RegisterBaseinfoVO;
import com.junzhiit.nengji.app.web.vo.RegisterPersonalVO;
import com.junzhiit.nengji.app.web.vo.ResultVO;
import com.junzhiit.nengji.app.web.vo.ShopInfoEditVO;

import io.swagger.annotations.ApiOperation;

@RestController
public class AuthorizeController extends BaseController{

	@Autowired
	private AuthorizeService authorizeService;
	
	@ApiOperation(value="商家登录")
	@RequestMapping(value="/shop/login",method = RequestMethod.POST)
	public ResultVO shopLogin(LoginVO loginVO,HttpServletResponse response) throws Exception{
		ResultVO result = new ResultVO();
		AssertUtil.notBlank(loginVO.getLoginName(), "login name is required.");
		AssertUtil.notBlank(loginVO.getLoginToken(), "login token is required.");
		AssertUtil.notNull(loginVO.getMethod(), "method is required.");
		AssertUtil.notNull(loginVO.getType(), "type is required.");
		result.setData(authorizeService.auth(loginVO,response));
		
		return result;
	}
	
	@ApiOperation(value="商家注销")
	@RequestMapping(value="/shop/logout", method = RequestMethod.POST)
	public ResultVO shopLogout(String sessionId,HttpServletRequest request,HttpServletResponse response) throws Exception{
		AssertUtil.notBlank(sessionId, "session id is required.");
		authorizeService.logout(sessionId,request,response);
		return new ResultVO();
	}
	
	@ApiOperation(value="获取手机验证码(注册)")
	@RequestMapping(value = "/get/mobile/phone/code", method = RequestMethod.GET)
	public ResultVO getMobilePhoneCode(String mobilePhone){
		AssertUtil.notBlank(mobilePhone, "mobile phone is required.");
		String smsModel=SmsUtil.ALIYUN_SMS_TEMPLATE_CODE;
		return SmsUtil.sendPhoneCode(mobilePhone, smsModel);
	}
	
	@ApiOperation(value="商家注册-基本信息(注册流程第一步)")
	@RequestMapping(value = "/shop/register/baseinfo",method = RequestMethod.POST)
	public ResultVO register(RegisterBaseinfoVO registerBaseinfoVO,HttpServletResponse response){
		ResultVO result = new ResultVO();
		AssertUtil.notBlank(registerBaseinfoVO.getRegisterToken(), "register token is required.");
		result.setData(authorizeService.registerBaseinfo(registerBaseinfoVO,response));
		return result;
	}
	
	@ApiOperation(value="商家注册-创建店铺(注册流程第二步)")
	@RequestMapping(value = "/shop/register/add",method = RequestMethod.POST)
	public ResultVO registerShopAdd(RegisterPersonalVO registerPersonalVO,String sessionId){
		AssertUtil.notBlank(sessionId, "sessionId is required.");
		return authorizeService.registerShopAdd(registerPersonalVO,sessionId);
	}
	
	@ApiOperation(value="商家注册-选择套餐(注册流程第三步)")
	@RequestMapping(value = "/shop/register/meal",method = RequestMethod.POST)
	public ResultVO registerMeal(String setMealId,String sessionId){
		AssertUtil.notBlank(setMealId, "setMealId is required.");
		AssertUtil.notBlank(sessionId, "sessionId is required.");
		return authorizeService.registerMeal(setMealId,sessionId);
	}
	
	@ApiOperation(value="商家注册-营业资质")
	@RequestMapping(value = "/shop/register/business",method = RequestMethod.PUT)
	public ResultVO registerBusiness(ShopInfoEditVO shopInfoEditVO){
		return authorizeService.registerBusiness(shopInfoEditVO);
	}
	
	@ApiOperation(value="找回密码 - 手机号码")
	@RequestMapping(value = "/retrieve/password/phone",method = RequestMethod.PUT)
	public ResultVO retrievePwdPhone(String passwordToken){
		AssertUtil.notBlank(passwordToken, "passwordToken is required.");
		return authorizeService.retrievePwdPhone(passwordToken);
	}
	
	@ApiOperation(value="获取手机验证码(找回密码)")
	@RequestMapping(value = "/getPhoneCodePwd", method = RequestMethod.GET)
	public ResultVO getPhoneCodePwd(String mobilePhone){
		AssertUtil.notBlank(mobilePhone, "mobile phone is required.");
		return authorizeService.getPhoneCodePwd(mobilePhone);
	}
	
	@ApiOperation(value="获取邮箱验证码(找回密码)")
	@RequestMapping(value = "/send/email", method = RequestMethod.GET)
	public ResultVO retrievePwdSetEmail(String email){
		AssertUtil.notBlank(email, "email is required.");
		return authorizeService.retrievePwdSetEmail(email);
	}
	
	@ApiOperation(value="找回密码 - 邮箱")
	@RequestMapping(value = "/retrieve/password/email",method = RequestMethod.PUT)
	public ResultVO retrievePwdEmail(String passwordToken){
		AssertUtil.notBlank(passwordToken, "passwordToken is required.");
		return authorizeService.retrievePwdEmail(passwordToken);
	}
	
}